package org.cestum.as3.collections {
import mx.collections.ArrayCollection;

import org.goverla.interfaces.IIterator;
import org.goverla.interfaces.IMap;


public class TreeMap implements IMap {
	public function get keys() : Array {
		var tempValues:ArrayCollection = new ArrayCollection();
		var unSortedKeys:ArrayCollection = new ArrayCollection(_keys.toArray());
		var sortedKeys:Array = _keys.source.sort();
		for (var index:String in _keys.source) {
			var i:int = int(index);
			var j:int= unSortedKeys.getItemIndex(sortedKeys[i]);
			tempValues.addItemAt(_values.getItemAt(j),i);
		}
		_values = tempValues;
		//toArray
		return sortedKeys;
	}
	
	public function get values() : Array {
		return _values.source;
	}
	
	public function addItem(key : Object, value : Object) : void {
		if (_keys.contains(key)) {
			_values.setItemAt(value, _keys.getItemIndex(key))
		} else {
			_keys.addItem(key);
			_values.addItem(value);
		}
	}
	
	public function removeItemAt(key : Object) : void {
		var index : int = _keys.getItemIndex(key);
		_keys.removeItemAt(index);
		_values.removeItemAt(index);
	}
	
	public function removeItem(value : Object) : void {
		var index : int = _values.getItemIndex(value);
		_keys.removeItemAt(index);
		_values.removeItemAt(index);
	}               
	
	public function getItem(key : Object) : Object {
		var index : int = _keys.getItemIndex(key);
		return (index > -1) ? _values.getItemAt(index) : null;  
	}
	
	public function valueIterator() : IIterator {
		//return new ListCollectionViewIterator(_values);
		throw new Error("Unimplemented Feature");
	}
	
	public function keyIterator() : IIterator {
		throw new Error("Unimplemented Feature");
	}
	
	public function clear() : void {
		_keys.removeAll();
		_values.removeAll();
	}
	
	public function containsKey(key : Object) : Boolean {
		return _keys.contains(key);
	}
	
	public function containsItem(value : Object) : Boolean {
		return _values.contains(value);
	}
	
	private var _keys : ArrayCollection = new ArrayCollection();
	
	private var _values : ArrayCollection = new ArrayCollection();    

}
}